//package com.example.sharding.config;
//
//
//import com.zaxxer.hikari.HikariDataSource;
//import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
//import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
//import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
//import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
//import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
////@Configuration
//public class ShardingConfig {
//
//    @Bean
//    public DataSource dataSource() throws SQLException {
//        // 配置真实数据源
//        Map<String, DataSource> dataSourceMap = new HashMap<>();
//        dataSourceMap.put("ds", createDataSource());
//
//        // 配置分片规则
//        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
//
//        // 配置t_order表的分片规则
//        ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration(
//                "t_order", "ds.t_order_$->{2023..2025}0$->{1..9},ds.t_order_$->{2023..2025}1$->{0..2}");
//
//        // 配置分表策略
//        StandardShardingStrategyConfiguration tableShardingStrategy = new StandardShardingStrategyConfiguration(
//                "create_time", "order-tbl-alg");
//        orderTableRuleConfig.setTableShardingStrategy(tableShardingStrategy);
//
//        // 配置主键生成策略
//        orderTableRuleConfig.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
//
//        shardingRuleConfig.getTables().add(orderTableRuleConfig);
//
//        // 配置主键生成器
////        Properties props = new Properties();
////        props.setProperty("worker-id", "123");
////        shardingRuleConfig.getKeyGenerators().put("snowflake",
////                new KeyGenerateAlgorithmConfiguration("SNOWFLAKE", props));
//
//        // 创建ShardingSphere数据源
//        return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap,
//                Collections.singleton(shardingRuleConfig), new Properties());
//    }
//
//    private DataSource createDataSource() {
//        HikariDataSource dataSource = new HikariDataSource();
//        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/sharding_db?useSSL=false&serverTimezone=UTC");
//        dataSource.setUsername("root");
//        dataSource.setPassword("123456");
//        dataSource.setMaximumPoolSize(20);
//        return dataSource;
//    }
//}
